package handler;

import java.io.File;
import java.util.Map;

import com.hn.comm.Tools;
import com.hn.comm.excel.ExcelHandler;
import com.hn.comm.excel.ExcelModel;
import com.hn.shoutchinese.voice.service.impl.VoiceServiceImpl;

public class VoiceHandler extends BaseHandler {

	public void startWork() {

		VoiceHandler voiceHandler = new VoiceHandler();

		// 得到父路径
		File fileExcel = new File(BaseHandler.getExcelFile());
		String fileParentPath = fileExcel.getParent();

		ExcelModel eModel = ExcelHandler.getExcelData(BaseHandler
				.getExcelFile());

		// 声音路径
		String filePath = fileParentPath + File.separator + eModel.getBookID();

		// 创建目录
		Tools.newFolder(filePath);

		// 下载服务
		VoiceServiceImpl vs = new VoiceServiceImpl();

		// 下载失败数
		int iFailCount = 0;
		int iCount = 0;
		for (Map<String, String> map : eModel.getContent()) {
			// 得到拼音
			String chinese = map.get(HEAD_CHINESE);
			// 文件名
			String voiceFile = filePath + File.separator + chinese + ".mp3";
			File fileVoice = new File(voiceFile);
			// 如果不存在该文件，那么下载

			if (!fileVoice.exists()) {
				System.out.println("正在下载," + chinese + ".");
				byte[] arrVoice = vs.getVoiceBytes(chinese);
				if (arrVoice != null) {
					vs.writeToFile(arrVoice, filePath, chinese + ".mp3");
					System.out.println("下载," + chinese + ",完成....");
				} else {
					System.out.println("xxx下载，" + chinese + ".失败");
					iFailCount++;
				}
			} else {
				System.out.println("已经存在," + chinese);
			}
			iCount++;
			System.out.println("count=" + iCount);
			int cf = 0;
			for (Map<String, String> map2 : eModel.getContent()) {
				String cfWord = map2.get(HEAD_CHINESE);
				if (cfWord.equals(chinese)) {
					if (cf > 0) {
						System.out.println(".........................重复次数" + cf
								+ ",重复单词=" + cfWord);
					}
					cf++;
				}
			}
		}
		System.out.println("语言下载结束,失败数" + iFailCount);
	}

	public static void main(String[] args) {
		VoiceHandler voiceHandler = new VoiceHandler();
		voiceHandler.openProxy();
		voiceHandler.startWork();
	}
}
